package com.yexs.wechat.util;

import com.yexs.common.enums.UserAuthScope;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * @Description: 微信相关常量
 * @Author: yexs
 * @CreateDate: 2018/5/27 19:05
 * @Version: 1.0
 */
@Component
public class Constant {

    //用户网页授权 基础url

    public static String baseAuthUrl;

    @Value("${baseAuthUrl}")
    public void setBaseAuthUrl(String baseAuthUrl) {
        Constant.baseAuthUrl = baseAuthUrl;
    }

    //    测试号信息
    // 第三方用户唯一凭证
    public static final String appID = "wx5aa6154173d5ede1";
    // 第三方用户唯一凭证密钥
    public static final String appSecret = "e50fca29c74414d3e326e5f080d27e1b";

    // 凭证获取（GET）
    private final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    // 获取用户信息（GET） lang：返回国家地区语言版本
    private final static String userInfo_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    //用户同意授权，获取code(网页授权：第一步)
    private final static String userAuthCode_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

    //通过code换取网页授权access_token(网页授权：第二步)
    //如果网页授权的作用域为snsapi_base，则本步骤中获取到网页授权access_token的同时，也获取到了openid，snsapi_base式的网页授权流程即到此为止
    private final static String authAccessToken_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";

    //刷新access_token（如果需要） refresh_token有效期为30天(网页授权：第三步)
    private final static String refreshAuthToken_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";

    //如果网页授权作用域为snsapi_userinfo，则此时开发者可以通过access_token和openid拉取用户信息了。(网页授权：第四步)
    private final static String snsUserInfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    //调用微信JS接口的临时票据，7200s有效期，需全局缓存
    private final static String jsapiTicket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

    public static String getTokenURL() {
        return token_url.replace("APPID", appID).replace("APPSECRET", appSecret);
    }

    public static String getUserInfoURL(String openId) {
        return userInfo_url.replace("ACCESS_TOKEN", CommonUtil.getToken().getAccessToken()).replace("OPENID", openId);
    }

    public static String getAuthAccessTokenURL(String code) {
        return authAccessToken_url.replace("APPID", appID).replace("SECRET", appSecret).replace("CODE", code);
    }

    public static String getAuthUserInfoURL(String accessToken, String openId) {
        return snsUserInfo_url.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);

    }

    public static String getUserAuthCodeURL(UserAuthScope scope) {
        return userAuthCode_url.replace("APPID", appID).replace("REDIRECT_URI", URLEncoder.encode(baseAuthUrl + "/wechat/getUserAuthInfo")).replace("SCOPE", scope.getValue());
    }

    public static String getJsapiTicketURL() {
        return jsapiTicket_url.replace("ACCESS_TOKEN", CommonUtil.getToken().getAccessToken());
    }
}
